var xhrs = [];
function ajx(data){
    
    var defaults = {
        success: function(data){
            $('#'+data.id).html(data.body);
          
        }
    };
   
    var xhr = $.ajax($.extend(defaults,data));
    xhrs.push(xhr);
};

function ajxClick (ev){
    //console.log(ev);
    if(ev)
        ev.preventDefault();
        
    ajx({
        url: this.href,
        dataType: "json"
    });
        
    return false;
};

function windowResize(){
 
    
    
};


function preloadElement(elm, fn, fnopt){
    
    var pre = $('.preloader');
    var elmp = pre.children('#p.loader').clone(true,true);
    var id = 'p'+$('.loader').length;
        
    elmp.attr('id', id);
    elm.prepend(elmp);
    
    $(id)[fn](fnopt);
    
    var n = $('#'+id);
    if(0==1){
        n.css({
            width: (elm.width())+'px',
            height: (elm.height())+'px'
        });
    }
    return n;
};

String.prototype.getParam = function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '/' + '([^\/]+)').exec(this)||[,null])[1]
        );
};





var fns = [];
var fnsI=0;

$(function(){
            
    $(window).unload(function(){
        for(var i in xhrs){
            xhrs[i].abort();
        }
    }); 
        
    $('.slider-nav-item a').click(function(ev){
        
        $('.slider-item').removeClass('active');
        var id = this.rel.replace(/[^0-9]+/,'');
        $('.slider-item.item'+id).addClass('active');
        $('.slider-nav-item').removeClass('active');
        $(this).parent().addClass('active');
    });
        
        
        
    $('dl').append('<div class="cls"></div>');
       
    
    $(window).resize(function(ev){
        windowResize();
    });
    
    
    
    $('.ui-button').click(function(){
        if($(this).find('a').length>0){
            window.location.assign($(this).find('a').attr('href'));
        }
    });
    
    $('.ajax').live('click',ajxClick);
    
    $('.ajax-post').live('click',function(ev){
        //console.log(ev);
        ev.preventDefault();        
        ajx({
            url: this.href,
            type: "POST",
            dataType: "json",
            data: $.parseJSON($('#ajx-pdata-'+this.rel).text()),
            content:$('#ajx-pdata-'+this.rel),
            success: function(data){
                if(data&&data.status=="ok"){
                    ajxClick.apply($('.ajax.current'),null)
                }
            }
        });
        
        return false;
    });
    
    
    $('.goback').click(function(ev){
        window.history.go(-1);
    });

    try{
        $(".markItUp").markItUp(mySettings);
    }catch(e){
        console.log(e);
    }

    
    
    setTimeout(function(){
        try{    
            fns[fnsI]();
            fnsI+=1;
        }catch(e){
        
        }
    },350);
     

     
});
